package co.edu.poli.ficb.agl.automatable.util;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Utils {
	
	private static Pattern pattern;
	
	static {
		pattern = Pattern.compile(".{1}(\\|.{1})*");
		System.out.println(pattern.pattern());
	}
	
	public static Set<Character> getConjuntoDeCaracteres(String s) {
		String b[] = s.split("\\|");
		Set<Character> set = new TreeSet<Character>();
		for (String c : b) {
			if (c.length()==1) 
				set.add(Character.valueOf(c.charAt(0)));
		}
		return set;
	}
	
	public static String conjuntoCaracteresAString(Set<Character> set) {
		StringBuffer sb = new StringBuffer();
		Iterator<Character> it = set.iterator();
		if (it.hasNext()) {
			sb.append(it.next());
		}
		while (it.hasNext()) {
			sb.append("|" + it.next());
		}
		return sb.toString();
	}

	public static boolean esEtiquetaValida(String etiqueta) {
		Matcher m = pattern.matcher(etiqueta);
		return etiqueta.isEmpty() || m.matches();
	}
	
	public static void main(String[] args) {
		System.out.println(esEtiquetaValida("asd"));
		System.out.println(esEtiquetaValida("a"));
	}

}
